package net.zjitc.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.zjitc.entity.Category;
import net.zjitc.exception.CategoryException;
import net.zjitc.mapper.CategoryMapper;
import net.zjitc.service.ICategoryService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-04-27 14:23:08
 * @describe 服务实现类
 */
@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Category> getListCategory(Integer type) throws CategoryException {
        if (type==null){
            type=3;
        }
        CategoryLevel level=CategoryLevel.getCategoryLevelEnum(type);
        if (level==null){
            throw new CategoryException("分类级别参数错误");
        }
        switch (level){
            case LEVEL_ONE:
                return getOneLeveList();
            case LEVEL_TWO:
                return getTwoLeveList();
            case LEVEL_THREE:
                return getThreeLeveList();
            default:
                throw new CategoryException("分类级别参数错误");
        }
    }

    @Override
    public Page<Category> getPageCateGory(Integer type, Integer pagenum, Integer pagesize) throws CategoryException {
        if (type==null){
            type=3;
        }
        CategoryLevel level = CategoryLevel.getCategoryLevelEnum(type);
        if (level==null){
            throw new CategoryException("分类级别参数错误");
        }
        switch (level){
            case LEVEL_ONE:
                return getOneLevePage(pagenum,pagesize);
            case LEVEL_TWO:
                return getTwoLevePage(pagenum,pagesize);
            case LEVEL_THREE:
                return getThreeLevePage(pagenum,pagesize);
        }
        return null;
    }

    private Page<Category> getThreeLevePage(Integer pagenum,Integer pagesize) {
        Page<Category> onelist=getPageByLevel(CategoryLevel.LEVEL_ONE.LEVEL-1,pagenum,pagesize);
        List<Category> twolist=getListByLevel(CategoryLevel.LEVEL_TWO.LEVEL-1);
        List<Category> threelist=getListByLevel(CategoryLevel.LEVEL_THREE.LEVEL-1);
        nodeCategoryToParent(twolist,threelist);
        nodeCategoryToParent(onelist.getRecords(),twolist);
        return onelist;
    }

    private Page<Category> getTwoLevePage(Integer pagenum,Integer pagesize) {
        Page<Category> onelist=getPageByLevel(CategoryLevel.LEVEL_ONE.LEVEL-1,pagenum,pagesize);
        List<Category> twolist=getListByLevel(CategoryLevel.LEVEL_TWO.LEVEL-1);
        nodeCategoryToParent(onelist.getRecords(), twolist);
        return onelist;
    }


    private Page<Category> getOneLevePage(Integer pagenum,Integer pagesize) {
        return getPageByLevel(CategoryLevel.LEVEL_ONE.LEVEL-1,pagenum,pagesize);
    }

    private Page<Category> getPageByLevel(Integer level,Integer pagenum,Integer pagesize){
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id,catename,pid,level,deleted");
        queryWrapper.eq("level",level);
        Page<Category> page=new Page<>(pagenum,pagesize);
        return categoryMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Category addLevel(Category category) {
        Integer level=category.getLevel();
        if (level==null){
            category.setPid(0);
        }else {
            category.setPid(category.getPid());
        }
        int insert = categoryMapper.insert(category);
        return category;
    }

    @Override
    public Category findById(Integer id) {
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id,catename,pid,level,deleted");
        queryWrapper.eq("id",id);
        Category category = categoryMapper.selectOne(queryWrapper);
        return category;
    }



    @Override
    public Integer deleteById(Integer id) {
        Category category = categoryMapper.selectById(id);
        int i=0 ;
        if (category.getLevel()==1){
            QueryWrapper<Category> wrapper=new QueryWrapper<>();
            wrapper.eq("pid",id);
            i = categoryMapper.delete(wrapper);
        }
        else if (category.getLevel()==0){
            QueryWrapper<Category> wrapper=new QueryWrapper<>();
            wrapper.eq("pid",id);
            List<Category> categories = categoryMapper.selectList(wrapper);
            for (Category category1: categories) {
                QueryWrapper<Category> wrapper1=new QueryWrapper<>();
                wrapper1.eq("pid",category1.getId());
                categoryMapper.delete(wrapper1);
                categoryMapper.deleteById(category1.getId());
            }
        }
        i = categoryMapper.deleteById(id);
        return i;
    }

    @Override
    public Category updateByid(Category category) {
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("id",category.getId());
        int update = categoryMapper.update(category, wrapper);
        return category;
    }

    //枚举
    public static enum CategoryLevel{
        LEVEL_ONE(1,"一级分类"),
        LEVEL_TWO(2,"二级分类"),
        LEVEL_THREE(3,"三级分类"),
        ;
        CategoryLevel(Integer LEVEL,String MESSAGE){
            this.LEVEL=LEVEL;
            this.MESSAGE=MESSAGE;

        }
        private final Integer LEVEL;
        private final String MESSAGE;

        public static CategoryLevel getCategoryLevelEnum(Integer type){
            for (CategoryLevel level:CategoryLevel.values()){
                if (level.LEVEL.intValue()==type.intValue()){
                    return level;
                }
            }
            return null;
        }
    }
    private List<Category> getListByLevel(Integer level){
        QueryWrapper<Category> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id,catename,pid,level,deleted");
        queryWrapper.eq("level",level);

        return categoryMapper.selectList(queryWrapper);
    }
    private void nodeCategoryToParent(List<Category> parentlsit, List<Category> nodelist) {
        for (Category parent: parentlsit) {
            List<Category> temp=new ArrayList<>();
            for (Category node: nodelist) {
                if(parent.getId().intValue()==node.getPid().intValue()){
                    temp.add(node);
                }
            }
            parent.setChildren(temp);
        }
    }
    private List<Category> getThreeLeveList() {
        List<Category> onelist=getListByLevel(CategoryLevel.LEVEL_ONE.LEVEL-1);
        List<Category> twolist=getListByLevel(CategoryLevel.LEVEL_TWO.LEVEL-1);
        List<Category> threelist=getListByLevel(CategoryLevel.LEVEL_THREE.LEVEL-1);
        nodeCategoryToParent(twolist,threelist);
        nodeCategoryToParent(onelist,twolist);
        return onelist;
    }

    private List<Category> getTwoLeveList() {
        List<Category> onelist=getListByLevel(CategoryLevel.LEVEL_ONE.LEVEL-1);
        List<Category> twolist=getListByLevel(CategoryLevel.LEVEL_TWO.LEVEL-1);
        nodeCategoryToParent(onelist, twolist);
        return onelist;
    }


    private List<Category> getOneLeveList() {
        return getListByLevel(CategoryLevel.LEVEL_ONE.LEVEL-1);
    }
}
